package com.share.device.controller;

import com.share.device.domain.dto.CabinetManagementDTO;
import com.share.device.domain.vo.CabinetManagementVO;
import com.share.device.service.IDeviceManagementService;
import com.share.common.core.web.controller.BaseController;
import com.share.common.core.web.domain.AjaxResult;
import com.share.common.log.annotation.Log;
import com.share.common.log.enums.BusinessType;
import com.share.common.security.annotation.RequiresPermissions;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 设备管理控制器
 * 
 * @author         
 * @date 2025-06-24
 */
@RestController
@RequestMapping("/management")
@Tag(name = "设备管理", description = "设备管理相关接口")
@Validated
public class DeviceManagementController extends BaseController
{
    @Autowired
    private IDeviceManagementService deviceManagementService;

    /**
     * 获取柜机管理详细信息
     */
    @RequiresPermissions("device:management:query")
    @GetMapping("/cabinet/{cabinetId}")
    @Operation(summary = "获取柜机管理详细信息")
    public AjaxResult getCabinetManagementInfo(@PathVariable("cabinetId") Long cabinetId)
    {
        CabinetManagementVO vo = deviceManagementService.getCabinetManagementInfo(cabinetId);
        return success(vo);
    }

    /**
     * 柜机部署
     */
    @RequiresPermissions("device:management:deploy")
    @PostMapping("/cabinet/deploy")
    @Operation(summary = "柜机部署")
    @Log(title = "柜机部署", businessType = BusinessType.UPDATE)
    public AjaxResult deployCabinet(@Valid @RequestBody CabinetManagementDTO dto)
    {
        boolean result = deviceManagementService.deployCabinet(dto);
        return toAjax(result);
    }

    /**
     * 柜机维护
     */
    @RequiresPermissions("device:management:maintain")
    @PostMapping("/cabinet/maintain")
    @Operation(summary = "柜机维护")
    @Log(title = "柜机维护", businessType = BusinessType.UPDATE)
    public AjaxResult maintainCabinet(@Valid @RequestBody CabinetManagementDTO dto)
    {
        boolean result = deviceManagementService.maintainCabinet(dto);
        return toAjax(result);
    }

    /**
     * 柜机移除
     */
    @RequiresPermissions("device:management:remove")
    @PostMapping("/cabinet/remove")
    @Operation(summary = "柜机移除")
    @Log(title = "柜机移除", businessType = BusinessType.UPDATE)
    public AjaxResult removeCabinet(@Valid @RequestBody CabinetManagementDTO dto)
    {
        boolean result = deviceManagementService.removeCabinet(dto);
        return toAjax(result);
    }

    /**
     * 柜机重置
     */
    @RequiresPermissions("device:management:reset")
    @PostMapping("/cabinet/reset")
    @Operation(summary = "柜机重置")
    @Log(title = "柜机重置", businessType = BusinessType.UPDATE)
    public AjaxResult resetCabinet(@Valid @RequestBody CabinetManagementDTO dto)
    {
        boolean result = deviceManagementService.resetCabinet(dto);
        return toAjax(result);
    }

    /**
     * 批量分配充电宝到插槽
     */
    @RequiresPermissions("device:management:assign")
    @PostMapping("/cabinet/{cabinetId}/assign-powerbanks")
    @Operation(summary = "批量分配充电宝到插槽")
    @Log(title = "分配充电宝", businessType = BusinessType.UPDATE)
    public AjaxResult assignPowerBanksToSlots(
            @PathVariable("cabinetId") Long cabinetId,
            @RequestParam("powerBankIds") List<Long> powerBankIds,
            @RequestParam("slotNos") List<String> slotNos)
    {
        boolean result = deviceManagementService.assignPowerBanksToSlots(cabinetId, powerBankIds, slotNos);
        return toAjax(result);
    }

    /**
     * 批量移除充电宝
     */
    @RequiresPermissions("device:management:remove-powerbanks")
    @PostMapping("/cabinet/{cabinetId}/remove-powerbanks")
    @Operation(summary = "批量移除充电宝")
    @Log(title = "移除充电宝", businessType = BusinessType.UPDATE)
    public AjaxResult removePowerBanksFromSlots(
            @PathVariable("cabinetId") Long cabinetId,
            @RequestParam("slotNos") List<String> slotNos)
    {
        boolean result = deviceManagementService.removePowerBanksFromSlots(cabinetId, slotNos);
        return toAjax(result);
    }

    /**
     * 获取设备状态统计
     */
    @RequiresPermissions("device:management:statistics")
    @GetMapping("/statistics")
    @Operation(summary = "获取设备状态统计")
    public AjaxResult getDeviceStatusStatistics()
    {
        Map<String, Object> statistics = deviceManagementService.getDeviceStatusStatistics();
        return success(statistics);
    }

    /**
     * 获取区域设备分布统计
     */
    @RequiresPermissions("device:management:distribution")
    @GetMapping("/distribution/{areaId}")
    @Operation(summary = "获取区域设备分布统计")
    public AjaxResult getAreaDeviceDistribution(@PathVariable("areaId") Long areaId)
    {
        Map<String, Object> distribution = deviceManagementService.getAreaDeviceDistribution(areaId);
        return success(distribution);
    }

    /**
     * 设备状态监控
     */
    @RequiresPermissions("device:management:monitor")
    @GetMapping("/monitor")
    @Operation(summary = "设备状态监控")
    public AjaxResult monitorDeviceStatus(
            @RequestParam("deviceType") String deviceType,
            @RequestParam(value = "status", required = false) String status)
    {
        List<Map<String, Object>> deviceList = deviceManagementService.monitorDeviceStatus(deviceType, status);
        return success(deviceList);
    }
} 